/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hello;

import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import ui.ScreenPainter;

/**
 *
 * @author Administrator
 */
public class TestCanvas extends GameCanvas implements CommandListener {

    //private Command resumeCommand = new Command("Resume", Command.OK, 1);
    private ScreenPainter screenPainter;
    private Image[] images = new Image[5];
    private boolean pause = false;
    //String url = "http://www.google.com.eg/images/nav_logo3.png";
    String url = "http://upload.wikimedia.org/wikipedia/en/thumb/c/c2/Android-logo.svg/500px-Android-logo.svg.png";

    public TestCanvas() {
        super(false);
        try {
            images[0] = Image.createImage("/resources/1.png");
            images[1] = Image.createImage("/resources/2.png");
            images[2] = Image.createImage("/resources/3.png");
            images[3] = Image.createImage("/resources/4.png");
            images[4] = Image.createImage("/resources/5.png");

//            images[0] = ImageLoader.getImageFromUrl(url);
//            images[1] = ImageLoader.getImageFromUrl(url);
//            images[2] = ImageLoader.getImageFromUrl(url);
//            images[3] = ImageLoader.getImageFromUrl(url);
//            images[4] = ImageLoader.getImageFromUrl(url);

            screenPainter = new ScreenPainter(this.getWidth(), this.getHeight());
            loopImages();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void loopImages() {
        Thread t = new Thread() {

            public void run() {
                try {
                    while (!pause) {
                        for (int i = 0; i < images.length && !pause; i++) {
                            screenPainter.Paint(getGraphics(), images[i], 0);
                            repaint();
                            Thread.sleep(500);
                            if (i == 4 && !pause) {
                                Thread.sleep(1000);
                            }
                        }
                    }
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        };
        t.start();
    }

    public void keyRepeated(int keyCode) {
        keyPressed(keyCode);
    }

    public void keyPressed(int keyCode) {
        try {
            System.out.println("key code " + keyCode);
            if (keyCode == -7 || keyCode == -6 || keyCode == 35 || keyCode == 42) {
                if (this.pause == true) {

                    this.pause = false;
                    loopImages();
                }
            } else {

                this.pause = true;
                screenPainter.Paint(getGraphics(), images[4], keyCode);
                this.repaint();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        super.keyPressed(keyCode);
    }

    public void commandAction(Command command, Displayable arg1) {
        //if (command.equals(resumeCommand)) {
        //}
    }
}
